#include "client_connet.h"
#include "interface_control.h"
Client_connet*Client_connet::m_Client = nullptr;
Client_connet::Client_connet()
    :m_fd(nullptr)
    ,m_obj(nullptr)
{
    Connect_Server();
    connect(m_fd,&QTcpSocket::readyRead,this,&Client_connet::readData);
}

void Client_connet::readData()
{

     //判断是否可读
    clearM_obj();
    if(!this->m_fd->isValid())
    {
        qDebug() << "unreadable";
        return;
    }
    // 从socket读取的数据
    qint64 total = this->m_fd->bytesAvailable();
    char buf[total];
    while(0 < this->m_fd->readLine(buf,sizeof(buf) + 1))
    {
        QString line = QString::fromUtf8(buf);

        // 解析Json数据
        QJsonObject obj = this->parserStr(line);
        if(line != nullptr)
        {
            *m_obj = obj;
        }
        qDebug()<<obj;
    }


    //信息处理
    QJsonObject js = *(Client_connet::Get_Connet()->getM_obj());
//    qDebug()<<js;
    int cmd = js["cmd"].toInt();
    qDebug()<<"cmd"<<cmd;
    switch (cmd)
    {

        case  0 :
        {
            if(js["result"].toInt() == -1)
            {
                //注册失败
                Interface_Control::getControlptr()->getm_enr()->enrollment_tips(-1);
            }
            else
            {
                Interface_Control::getControlptr()->getm_login()->setID(js["loginid"].toString());
                Interface_Control::getControlptr()->getm_enr()->enrollment_tips(1);
            }
        }
        break;

        case 1 :
        {
            if(js["result"].toInt() == 1)
            {
                qDebug()<<"断点9";
                //登录完成设置个人信息
                qDebug()<<"读取测试";
                QJsonArray fe = js["friends"].toArray();
                QJsonObject fed;
                qDebug()<<"ARRAY长度:"<<fe.size();
                for(int i = 0; i < fe.size(); ++i)
                {
                    friends f;
                    fed = fe[i].toObject();
                    f.name = fed["name"].toString();
                    f.friendloginid =fed["friendloginid"].toString();
                    f.FriendGroups = fed["FriendGroups"].toInt();

                    frarr.push_back(f);

                }
                //配置用户信息
                fe = js["user"].toArray();
                fed = fe[0].toObject();
                user u;
                u.ID = fed["loginid"].toString();
                u.name = fed["nickname"].toString();
                me = u;
                qDebug()<<"用户配置完成";

                //完成后界面跳转
                Interface_Control::getControlptr()->showm_fr();
                Interface_Control::getControlptr()->hideLogin();
            }
        }
        break;

        case 2 :
        {
            if(js["result"] == 5)
            {
                qDebug()<<"打印提示，对方不在线";
                //测试
                QString str = "对方不在线";
                //测试
                qDebug()<<str;
    //            Interface_Control::getControlptr()->get_m_gro()->recvMessage(str);
            }
            else
            {
                QString str = js["message"].toString();
                QString ID = js["sender"].toString();
                Interface_Control::getControlptr()->geto_map(ID)->recvMessage(str);
                qDebug()<<"好友聊天信息发送成功";
            }
        }
        break;

    case 5:
        {
               if(js["result"] == 9)
               {
                   QJsonArray ue = js["user"].toArray();
                   QJsonObject ued;
                   qDebug()<<"ARRAY长度:"<<ue.size();
                   for(int i = 0; i < ue.size(); ++i)
                   {
                       user u;
                       ued = ue[i].toObject();
                       u.name = ued["nickname"].toString();
                       u.ID =ued["loginid"].toString();

                       //插入界面
                       Interface_Control::getControlptr()->getm_fr()->getfindu()->AddFriend(u.ID,u.name);

                   }
               }
               else
               {
                   qDebug()<<"查询好友为空";
               }
        }
        break;

    case 6 :
        {
            if(js["result"] == 7)
            {
                Interface_Control* end;
                end = Interface_Control::getControlptr();
                delete  end;
            }
            else
            {
                qDebug()<<"关闭失败\n";
            }
        }



    }




}

